<template>
	<div class="page">
		<div class="logo">
			<img class="logo-img" src="../assets/userLogo-2.png" alt="" />
			<div class="right-ic" @click="changeLanguage">
				<img src="../assets/100-1.png" alt="" />
				<span>{{$i18n.locale == 'zh' ? 'EN' : '中文'}}</span>
			</div>
		</div>
		<div class="content">
			<div class="search">
				<input type="text" v-model="search" :placeholder="$t('language.search')" />
				<img class="ic" src="../assets/101.png" alt="" />
			</div>
			<div class="tab-title" v-if="type !== 4">
				<!-- {{$i18n.locale == 'zh' ? userinfo.hotel.title : userinfo.hotel.title_en }} -->
				{{hotel_name}}
			</div>
			
			<van-list v-model="loading" :finished="finished" :finished-text="$t('language.noMore')" @load="onLoad">
				<div class="card" v-for="(item, index) in list" :key="item.id">
					<div class="user-name" v-if="item.user">
						{{item.user.name}}
						<span class="en">{{ item.user.english_name }}</span>
						<!-- <span class="tag">{{item.user.position}}</span> -->
					</div>
					<div class="tag-wrap">
						<!-- is_verify 0 待审核 1 成功 2 失败 -->
						<div v-if="item.is_verify == 0" class="tag await">{{$t('language.text12')}}</div>
						<div v-if="item.is_verify == 1" class="tag success">{{$t('language.text13')}}</div>
						<div v-if="item.is_verify == 2" class="tag not">{{$t('language.text14')}}</div>
						<!-- <div class="tag">长期客户</div> -->
					</div>
					<div class="info-wrap">
						<div class="title">{{item.title}}</div>
						<div class="title small">{{item.title_alias}}</div>
						<div class="info top-10">{{$t('language.text7')}}: {{item.agreement_price_code}}</div>
						<div class="info top-10">{{$t('language.text54')}}: <span class="num">{{item.price_code}}</span></div>
						<template v-if="item.is_verify == 0">
							<div class="btn not" @click="tapShowPopup(item.id, index)">{{$t('language.text14')}}</div>
							<div class="btn" @click="tapCheck(item.id, index, 1)">{{$t('language.text13')}}</div>
						</template>
					</div>
					
					<!-- <div class="tap-lnr">
						<van-switch @click="tapShare(item)" v-model="item.is_share" active-color="#4BD865" />
						<span>{{$t('language.text29')}}</span>
					</div> -->
				</div>
			</van-list>
		</div>
			
		<div class="add-btn">
			<div class="btn" @click="goPage('customer')">
				<img src="../assets/tab-2.png" alt="" />
				<span>{{type == 2 ? $t('language.text34') : $t('language.text85')}}</span>
			</div>
			<div class="btn" @click="goPage('sell')">
				<img src="../assets/tab-1.png" alt="" />
				<span>{{$t('language.text33')}}</span>
			</div>
			<!-- <div class="btn" @click="goPage('assign')">
				<img src="../assets/tab-3.png" alt="" />
				<span>{{$t('language.text35')}}</span>
			</div> -->
			<div class="btn select">
				<img src="../assets/tab-selcet-4.png" alt="" />
				<span>{{$t('language.text36')}}</span>
				<div class="count" v-if="count > 0">
					<span>{{count > 9 ? 9 : count}}</span>
					<span v-if="count > 9" class="add-ic">+</span>
				</div>
			</div>
			<div class="btn" @click="goPage('forms')">
				<img src="../assets/tab-5.png" alt="" />
				<span>{{$t('language.text37')}}</span>
			</div>
		</div>
		
		<van-popup v-model="showPopup ">
			<div class="popup-title">{{$t('language.text47')}}</div>
			<textarea v-model="verifyText" :placeholder="$t('language.text48')" class="popup-content" cols="30" rows="10"></textarea>
			<div class="popup-btn">
				<div class="btn" @click="tapCheck(lnrId,lnrIndex, 2)">{{$t('language.text9')}}</div>
				<div class="btn back" @click="showPopup = !showPopup">{{$t('language.text25')}}</div>
			</div>
		</van-popup>
	</div>
</template>
<script>
	import {
		getUserInfo,
		getHotelLnr,
		setLnrShare,
		lnrVerify,
		verifyCount
	} from '@/api/user';
	import {
		Dialog,
		Toast 
	} from "vant";
	let timer = null
	
	export default {
		name: 'Check',
		data: function() {
			return {
				locale: 'zh',
				search: '',
				hotel_name: '',
				verifyText: '',
				checked: false,
				type: 2,
				showPopup: false,
				userinfo: {
					hotel: {}
				},
				lnrId: '',
				lnrIndex: 0,
				list: [],
				loading: false,
				finished: false,
				notTap: false,
				page: 1,
				count: 0
			};
		},
		created() {
			let _hotels = localStorage.getItem('hotels')
			if (_hotels) {
				_hotels = JSON.parse(_hotels)
				this.hotel_name = _hotels.hotel_name
			}
			const userinfo = localStorage.getItem("userinfo")
			const {
				type
			} = JSON.parse(userinfo)
			this.type = type
			this.getUserInfoData()
			this.getVerifyCount()
		},
		mounted() {},
		watch: {
			search: function() {
				const t = this
				if (timer != null) clearTimeout(timer)
		
				timer = setTimeout(() => {
					timer = null
					clearTimeout(timer)
					t.page = 1
					t.list = []
					t.getHotelLnrData()
				}, 500)
			}
		},
		methods: {
			onLoad() {
				this.getHotelLnrData()
			},
			getHotelLnrData() {
				getHotelLnr({
					page: this.page,
					key: this.search
				}).then((res) => {
					let {
						current_page,
						last_page,
						data
					} = res.data
					data.forEach((item)=>{
						item.is_share === 0 ? item.is_share = false : item.is_share = true
					})
					// 加载状态结束
					this.loading = false;
					// 数据全部加载完成
					if (current_page == last_page) {
						this.finished = true;
					} else {
						this.finished = false;
						this.page++
					}
					this.list = [...this.list, ...data]
				}).catch((err) => {
					this.loading = false;
					console.log(err)
				})
			},
			tapShowPopup(id, index) {
				this.lnrId = id
				this.lnrIndex = index
				this.showPopup = !this.showPopup
			},
			tapShare(item) {
				let isShow = 0
				if(!item.is_share) {
					isShow = 1
				} 
				setLnrShare({
					is_share: isShow,
					lnr_id: item.id
				}).then((res) => {
					if (res.status_code != 200) {
						Toast(res.message)
					} else {
						Toast(this.$i18n.tc('language.text66'))
					}
				}).catch((err) => {
					console.log(err)
				})
			},
			tapCheck(id, index, verify) {
				const t = this
				if(t.notTap) return
				
				if(verify == 2 && t.verifyText == '') return Toast(t.$i18n.tc('language.text48'));
				t.notTap = true
				timer = setTimeout(()=>{
					t.notTap = false
					clearTimeout(timer)
					timer = null
				}, 1000)
				
				lnrVerify({
					lnr_id: id,
					verify_msg: t.verifyText,
					is_verify: verify
				}).then((res) => {
					if (res.status_code == 200) {
						// Toast(t.$i18n.tc('language.text68'))
						Dialog.alert({
							title: this.$i18n.tc('language.text18'),
							message: '审核通过后，默认允许销售人员进行键分享。',
							theme: 'round-button',
							confirmButtonText: this.$i18n.tc('language.text20'),
						}).then(() => {});
						t.list.forEach((item, i)=>{
							if(item.id === id) {
								t.list[i].is_verify = verify
							}
						})
						this.$forceUpdate()
						this.showPopup = false
						return
					}
					Toast(res.message)
				}).catch((err) => {
					console.log(err)
				})
			},
			getVerifyCount() {
				verifyCount().then((res) => {
					this.count = res.data
				}).catch((err) => {
					console.log(err)
				})
			},
			getUserInfoData() {
				getUserInfo().then((res) => {
					this.userinfo = res.data
				}).catch((err) => {
					console.log(err)
				})
			},
			goPage(page, id) {
				this.$router.push({
					path: page,
					query: {
						id
					}
				})
			},
			changeLanguage() {
				if (this.$i18n.locale == 'zh') {
					this.$i18n.locale = 'en';
					this.$cookies.set('locale', 'en')
				} else {
					this.$i18n.locale = 'zh';
					this.$cookies.set('locale', 'zh')
				}
			}
		}
	};
</script>
<style type="text/css" lang="scss" scoped>
	.page {
		background-color: #fff;
	}

	.logo {
		background-color: #fff;

		.tab-app {
			display: flex;
			align-items: center;
			font-size: 24px;
			color: #333333;
			
			img {
				width: 31px;
				height: 30px;
				margin-right: 6px;
			}
		}

		.right-ic {
			color: #333333;
		}
	}

	.content {
		padding-bottom: 114px;
		border-top: 8px solid #f2f5f7;

		.search {
			padding-top: 40px;
			padding-bottom: 16px;
			margin: 40px 30px;
			position: relative;
			z-index: 10;
			padding: 0 28px;
			height: 74px;
			margin-left: 24px;
			border: 2px solid #1f4456;
			background-color: #fff;
			box-sizing: border-box;
			border-radius: 45px;
			display: flex;
			align-items: center;
			justify-content: space-between;
		
			input {
				font-size: 24px;
				color: #666;
				width: 100%;
				height: 100%;
			}
		
			.ic {
				width: 28px;
				height: 28px;
				flex-shrink: 0;
			}
		}

		.tab-title {
			margin: 0 30px;
			font-size: 36px;
			color: #1F4456;
			font-weight: bold;
		}

		.card {
			margin: 24px 30px 0;
			padding: 30px;
			box-sizing: border-box;
			border-radius: 10px;
			background-color: #fff;
			box-shadow: 0 0 24px #eee;
			position: relative;

			.title {
				font-size: 36px;
				color: #1F4456;
				font-weight: bold;
				&.small {
					font-size: 24px;
					margin-top: 8px;
				}
			}

			.tag-wrap {
				display: flex;
				flex-wrap: wrap;

				.tag {
					padding: 8px 18px;
					border-radius: 0 8px 8px 8px;
					color: #e8542c;
					font-size: 22px;
					margin-right: 12px;
					background-color: #fbe8e2;

					&.not {
						color: #fff;
						background-color: #f01a1a;
					}
					&.success {
						color: #fff;
						background-color: #09bb00;
					}
					&.await {
						color: #fff;
						background-color: #e8542c;
					}
				}
			}
			
			.user-name {
				padding-bottom: 24px;
				font-size: 30px;
				color: #333333;
				font-weight: bold;
				
				.tag {
					position: relative;
					top: 4px;
					color: #1f4456;
					margin-left: 12px;
					display: inline-block;
					max-width: 300px;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
					padding: 8px 12px;
					border-radius: 18px 18px 18px 0;
					font-size: 20px;
					background-color: #EDF2F5;
				}
				
				.en {
					margin-left: 10px;
				}
			}
			
			.info-wrap {
				margin-top: 24px;
				position: relative;

				.info {
					font-size: 24px;
					color: #666666;

					.num {
						color: #1f4456;
					}
					
					&.top-10 {
						margin-top: 10px;
					}
				}
				
				.btn {
					position: absolute;
					right: 0;
					bottom: 0;
					color: #fff;
					font-size: 24px;
					width: 176px;
					height: 54px;
					line-height: 54px;
					text-align: center;
					padding: 0 24px;
					border-radius: 10px;
					background-color: #E8542C;
				
					&.not {
						bottom: 80px;
						background-color: #1f4456;
					}
				}
			}
			
			.tap-lnr {
				display: flex;
				align-items: center;
				color: #999999;
				font-size: 24px;
				padding-top: 40px;
				margin-top: 24px;
				position: relative;
			
				span {
					padding-left: 12px;
				}
			
				.ic {
					width: 54px;
					height: 54px;
					position: absolute;
					top: 84%;
					right: 0;
					transform: translateY(-50%);
			
					&.not {
						width: 38px;
						height: 38px;
					}
				}
			}
		}
	}

	.add-btn {
		position: fixed;
		left: 50%;
		bottom: 0;
		transform: translateX(-50%);
		width: 100%;
		height: 114px;
		max-width: 750px !important;
		color: #fff;
		font-size: 28px;
		background-color: #fff;
		box-shadow: 0 2px 24px #eee;
		padding: 14px 30px;
		display: flex;
		justify-content: space-around;
		align-items: center;
		text-align: center;
		
		.btn {
			color: #999999;
			font-size: 24px;
			display: flex;
			flex-direction: column;
			justify-content: flex-start;
			align-items: center;
			max-width: 18%;
			margin: 0 12px;
			height: 100%;
			position: relative;

			&.select {
				color: #1f4456;
			}

			img {
				width: 38px;
				height: 38px;
				margin-bottom: 8px;
				object-fit: contain;
			}
			.count {
				position: absolute;
				top: -10px;
				right: 0;
				width: 40px;
				height: 40px;
				border-radius: 50%;
				line-height: 44px;
				text-align: center;
				font-size: 22px;
				color: #FFFFFF;
				background-color: #E8542C;
				.add-ic {
					font-size: 20px;
					position: relative;
					top: -8px;
				}
			}
		}
	}

	.van-popup {
		width: 80%;
		padding: 80px 58px;
		border-radius: 10px;
		box-sizing: border-box;

		.popup-title {
			font-size: 36px;
			color: #0A0204;
			margin-bottom: 30px;
			font-weight: 800;
			text-align: center;
		}

		.popup-content {
			width: 100%;
			height: 222px;
			background: #EEEEEE;
			font-size: 24px;
			color: #666666;
			border-radius: 10px;
			padding: 15px;
			border: none;
			box-sizing: border-box;
		}

		.popup-btn {
			display: flex;
			align-content: center;
			justify-content: space-between;
			margin-top: 24px;
			
			.btn {
				width: 48%;
				height: 72px;
				line-height: 72px;
				text-align: center;
				background: #1F4456;
				border-radius: 10px;
				font-size: 24px;
				font-weight: bold;
				color: #FFFFFF;
				&.back {
					background: #FFFFFF;
					border: 2px solid #1F4456;
					border-radius: 10px;
					color: #1F4456;
				}
			}
		}
	}
</style>